// pages/hole/hole_info/hole_info.js
const app = getApp()
const mta = require('../../../mta_sdk/mta_analysis.js');
Page({

  data: {
    id:'',
    info:[],
    commentList:[],
    isLoadComment:false,
    showLogin:false,
    tempDataSet:{},
    isDelete:false,
    itemIndex:-1,
    zanNum:0,
    isZan:false,
    replayName:'',
    commentClick:false,
    showComment:false,
    commentVal:'',
    uid:'',
    currPage:1,
    nodata:false
  },

  onLoad: function (options) {
    this.getInfo(options.id);
    this.setData({
      id:options.id,
      uid:wx.getStorageSync('userData').uid || ''
    });
    mta.Page.init();
  },
  getInfo(id){
    app.ajax({
      url: "Game/complain_info",
      data:{
        id:id,
      }
    }).then(res => {
      this.setData({
        info:[res.data.complain]
      });
      if(!this.data.isLoadComment){
        this.getCommentList(id,1);
      }
      
    })
  },
  getCommentList(id,currPage){
    app.ajax({
      url: "Game/complain_comment_list",
      data:{
        id:id,
        page:currPage
      },
      loading:true
    }).then(res => {
      this.setData({
        commentList:currPage==1?res.data.list:[...this.data.commentList,...res.data.list],
        isLoadComment:true,
        nodata:res.data.list.length<10?true:false
      });
      wx.stopPullDownRefresh();
    })
  },
  getStart(e){
    let type=e.currentTarget.dataset.type
    app.userLogin(e.detail.userInfo,()=>{
      if(type==2 && !this.data.commentClick){
        let e={
          currentTarget:{
            dataset:this.data.tempDataSet
          }
        }
        this.addZan(e);
      }
      //如果是点击评论
      else{
        this.setData({
          showComment:true
        })
      }
      this.setData({
        showLogin:false
      });
    })
  },
  formSubmit(e){
    app.sendForm(e);
  },
  addZan(e){
    let dataset=e.currentTarget.dataset;
    let[uid,itemid,iszan]=[dataset.uid,dataset.itemid,dataset.iszan];
    if(iszan==1){
      app.toast("已经赞过了~");
      return false;
    }
    app.ajax({
      url:"Game/complain_zan",
      data:{
        uid:uid,
        id:itemid
      }
    }).then(res => {
      if(res.code==200){
        this.getInfo(this.data.id);
      }
    });
  },
  //d点击评论弹出输入框
  showCommentInput(e){
    
    let dataset=e.currentTarget.dataset;
    let uid=wx.getStorageSync("userData").uid;
      this.setData({
        showLogin:uid?false:true,
        commentClick:uid?false:true,
        tempDataSet:dataset,
        showComment:uid?true:false
      })
  },
  bindKeyInput(e){
    this.setData({
      commentVal:e.detail.value
    })
  },
  inputOut(){
    if(!this.data.commentVal){
      this.setData({
        showComment:false,
        commentClick:false
      })
    }
  },
  //提交评论
  subComment(){
    
    let val=this.data.commentVal;
    let sets=this.data.tempDataSet;
    let[itemid]=[sets.itemid];
    console.log(sets)
    if(val==''){
      app.toast('内容不能为空哦~');
    }
    else if(val.length<2){
      app.toast('内容太短啦。');
    }
    else{
      app.ajax({
        url:"Game/complain_comment_save",
        loading:true,
        loadTip:'提交中...',
        data:{
          id:itemid,
          type:0,
          comment:val
        }
      }).then(res => {
        if(res.code==200){
          app.toast('评论成功。');
          this.setData({
            showComment:false,
            isLoadComment:false,
            commentClick:false
          });
        this.getInfo(this.data.id);
        }
        else{
          app.toast(res.msg);
        }
      })
    }
  },
    //删除评论
    removeItem(e){
      let dataset=e.currentTarget.dataset;
      let[itemid,complainid]=[dataset.itemid,dataset.complainid];
      wx.showModal({
        title: '温馨提示',
        content: '您确定要删除吗？',
        success: result => {
          if(result.confirm){
            app.ajax({
              url: "Game/complain_comment_del",
              loading:true,
              data:{
                id:itemid,
                complain_id:complainid
              }
            }).then(res => {
              app.toast('删除成功~');
              this.setData({
                isLoadComment:false,
                currPage:1
              });
              setTimeout(()=>{
                this.getInfo(this.data.id);
              },1500);
              
            });
          }
        }
      });
    },
  onPullDownRefresh: function () {
    this.setData({
      isLoadComment:false
    })
    this.getInfo(this.data.id);
  },

  onReachBottom: function () {
    if(this.data.nodata){
      return false;
    }
    let page=++this.data.currPage;
    this.getCommentList(this.data.id,page);
  },

  onShareAppMessage: function () {
  
  }
})